package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class DishServicelmpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加菜品口味
     * @param dishDTO
     */
    @Override
    public void AddDishFlover(DishDTO dishDTO) {

        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        //因为菜品会关联一个口味表 口味表是根据菜品的id添加的
        //但因为插入数据库后 并不能直接从实体类拿取 所以会在xml的insert定义useGeneratedKeys="true" keyProperty="id"
        //让他插入成功后返回id字段赋值给实体类
        //这里result只是判断是否成功，不是赋值给他
        dishMapper.AddDish(dish);

        Long id = dish.getId();

        //构造List接受菜品口味列表
        List<DishFlavor> flavorList=dishDTO.getFlavors();
        if(flavorList!=null && flavorList.size()!=0){
            //口味不为空时

            //把每个口味设置这个菜品的ID 标注是这个菜品的
            flavorList.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });
            //遍历插入菜品口味
            dishFlavorMapper.AddFlavor(flavorList);
        }
    }

    /**
     * 菜品查询
     * @param dishPageQueryDTO
     * @return
     */
    public PageResult DishPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page=dishMapper.DishQueryPage(dishPageQueryDTO);
        PageResult pageResult=new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return pageResult;
    }

    /**
     * 删除菜品
     * @param ids
     */
    @Override
    public void DeleteDish(List<Long> ids) {
        //判断菜品状态是否为起售状态 起售状态不允许删除
        for (Long id : ids) {
            Dish dish=dishMapper.SeleteDishStatus(id);
            if(dish.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }


        //判断菜品是否关联了套餐 关联了不允许删除
            List<Long> setmealId=setmealMapper.DeleteSetmealDish(ids);
            if(setmealId!=null && setmealId.size()>0){
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }

        //删除菜品和他的口味
        for (Long id : ids) {
            //删除菜品
            dishMapper.DeleteDish(id);

            //删除菜品口味
            dishFlavorMapper.DeleteFlavorDish(id);
        }
    }

    /**
     * 起售停售菜品
     * @param status
     */
    @Override
    public void StartORForb(Integer status,Long id) {
        dishMapper.StartORForb(status,id);
    }

    /**
     * 根据ID查询菜品
     * @param id
     * @return
     */
    @Override
    public DishVO DishIDQuery(Long id) {
        //菜品由数据库的两个表组成，一个是菜品基本信息，一个是菜品的口味表信息
        //两个同时查出来，再把他俩的总和数据 VO 结合到一起返回

        //查菜品基本信息
        Dish dish=dishMapper.SeleteDishStatus(id);

        //查询指定菜品的口味表
        List<DishFlavor> dishFlavors=dishFlavorMapper.SelectFlavor(id);

        //把两者结合
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);

        return dishVO;
    }

    /**
     * 修改新的菜品信息
     * @param dishDTO
     */
    @Override
    public void UpdateNewDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //修改菜品基本信息
        dishMapper.UpdateDish(dish);

        //删除菜品的口味表
        dishFlavorMapper.DeleteFlavorDish(dishDTO.getId());

        //添加新的口味表
        //构造List接受菜品口味列表
        List<DishFlavor> flavorList=dishDTO.getFlavors();
        if(flavorList!=null && flavorList.size()!=0){
            //口味不为空时

            //把每个口味设置这个菜品的ID 标注是这个菜品的
            flavorList.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            //遍历插入菜品口味
            dishFlavorMapper.AddFlavor(flavorList);
        }
    }


}
